

*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*
*   Subject:							Political Science
*	Module:         					Empirical methods 2
*   Lecture:  							Praxiskurs quantitative Datenanalyse und Replikation
*	Date:								22.05.2023

*   Name:								Niklas Maximilian Reinecke
*	Matrikelnummer/Registration number: 4172903
*	Hausarbeit/Essay:					Replication of Levin et al. (2021)
*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*

*********************************************************************************************Setup commands*********************************************************************************************
clear all
version 17.0
set more off 
*cd "C:\Users\nikma\OneDrive\Shared Dokumente\Dokumente\Replicationsmaterialien" //Working directory LapTop
*cd "D:\OneDrive\Shared Dokumente\Dokumente\Replicationsmaterialien" //Working directory PC

cd "C:\Users\ba72loko\projects\replication paper ZfVP\calculations"

capture log close //optional
log using "Study_2_log_file.log", replace //output log


*** Loading dataset

use "data\winning_coalition_data.dta", clear //opening pre-processed winning_coalition_data from V-Dem (see R-script for the dataset construction)
describe

gsort country_id year

*deleting all observations from before 1991 and after 2013
drop if year < 1991
drop if year > 2013

gsort country_id year

***Merging Data***
merge m:m year country_id using "data\V-Dem-Replication.dta"

*Poisson regression, winning coalition size as main IV with year fixed effects and controlled for regime type and most important support group

est clear

eststo: poisson milcoupsum c.winning_coalition_norm##c.troops gdpcap durable conflict milexp exsol population2000 i.year, cluster(ccode)


esttab using "tables\Study_2\Main_Table.rtf", b(3) se(3) star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
title(Table 2: Determinants of all coups, 1991 to 2013 Poisson regression with year fixed effects) ///
drop(*.year) ///
label varlabels(winning_coalition_norm "Winning Coalition Size"  troops "Peacekeepers" c.winning_coalition_norm#c.troops "Winning Coalition Size * Peacekeepers" ///
gdpcap "GDP pc" durable "Regime durability" conflict "Political violence" milexp "military expenditure" exsol "Expenditure per soldier" population2000 "Population" ///
 _cons "Constant") ///
order(winning_coalition_norm troops c.winning_coalition_norm#c.troops ///
gdpcap durable conflict milexp exsol population2000) nonumbers modelwidth(6) eqlabel(none) varwidth(33) replace

poisson milcoupsum c.winning_coalition_norm##c.troops gdpcap durable conflict milexp exsol population2000 i.year, cluster(ccode)
margins, dydx(troops) at(winning_coalition_norm=(0(0.01)1)) post
marginsplot, recast(line) ciopt(color(%20)) recastci(rarea) scheme(s2color) graphregion(color(white)) ///
    legend(region(color(white))) ///
    xtitle("Winning Coalition Size") title("")
graph export "figures/Study_2/Figure_2_Main_paper.png", replace



*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*
* Figure 2 Main Paper
*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*

*** Loading dataset
use "data\Study_2\original_data.dta", clear //opening replication dataset
describe

*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*
* Generating variables
*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*
**generating New robust democracy dummy variable**
gen new_robust_democracy6 = . //generating a new variable for the fixed version of robust_democracy6
replace new_robust_democracy6 = robust_democracy6
replace new_robust_democracy6 = 0 if polity2 == . //missings von polity2 als new_robust_autocracy6 == 0 codiert 


*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*
* Labeling of varlabels
*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*
label variable marshallsum "All coup attempts"
label variable milcoupsum "Military coup attempts"
label variable milcoupsuccess_mm "Successful military coups"
label variable troops "Peacekeepers"
label variable robust_autocracy6 "Robust autocracy"
label variable robust_anocracy5 "Anocracy"
label variable robust_democracy6 "Robust democracy"
label variable population2000 "Population"
label variable gdpcap "GDP per capita"
label variable durable "Regime durability"
label variable conflict "Political violence"
label variable milexp "Military expenditure"
label variable exsol "Expenditure per soldier"
label variable effective_number "Effective organizations"
label variable elf "Ethnic fractionalization"
label variable aut_troops "Autocracy Peacekeepers"
label variable ano_troops "Anocracy Peacekeepers"
label variable dem_troops "Democracy Peacekeepers"
label variable aut_troops_round "Autocracy Peacekeepers rounded"
label variable ano_troops_round "Anocracy Peacekeepers rounded"
label variable dem_troops_round "Democracy Peacekeepers rounded"
label variable new_robust_democracy6 "New robust democracy"


*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*
* Original estimates
*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*
	   
**FIGURE 1**
**model 1: autocracy
poisson milcoupsum robust_autocracy6 c.troops aut_troops_round population2000 gdpcap durable conflict milexp exsol, cluster(ccode)
margins, dydx(aut_troops_round) post
est store m1

**model 2: anocracy
poisson milcoupsum robust_anocracy5 c.troops ano_troops_round population2000 gdpcap durable conflict milexp exsol, cluster(ccode)
margins, dydx(ano_troops_round) post
est store m2

**model 3: democracy
eststo: poisson milcoupsum robust_democracy6 c.troops dem_troops_round population2000 gdpcap durable conflict milexp exsol, cluster(ccode)
margins, dydx(dem_troops_round) post
est store m3

*coefplot
coefplot (m1, label(Autocracy * Peacekeepers)) ///
(m2, label(Anocracy * Peacekeepers)) ///
(m3, label(Democracy * Peacekeepers)), xline(0) ///
title("", size(medsmall) span) xtitle("Average Marginal Effects", size(small)) ylabel(,labsize(vsmall)) xlabel(,labsize(vsmall)) legend(off) title("A", position(11))

graph save "figures\Study_2\Figure_2_original.gph", replace


*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*
* Correctly estimated AME
*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*
	
*model 2a: autocracy
poisson milcoupsum robust_autocracy6##c.troops population2000 gdpcap durable conflict milexp exsol, cluster(ccode)
margins, dydx(robust_autocracy6) post
est store m2a


*model 4a: anocracy
poisson milcoupsum robust_anocracy5##c.troops population2000 gdpcap durable conflict milexp exsol, cluster(ccode)
margins, dydx(robust_anocracy5) post
est store m4a


*model 8a: New democracy
eststo: poisson milcoupsum new_robust_democracy6##c.troops population2000 gdpcap durable conflict milexp exsol, cluster(ccode)
margins, dydx(new_robust_democracy6) post
est store m8a


*coefplot
coefplot (m2a, label(Autocracy)) ///
(m4a, label(Anocracy)) ///
(m8a, label(Democracy, corrected)), xline(0) ///
title("", size(medsmall) span) xtitle("Average Marginal Effects", size(small)) ylabel(,labsize(vsmall)) xlabel(,labsize(vsmall)) legend(off) title("B", position(11))


graph save "figures\Study_2\Figure_2_AME_Regime_Type.gph", replace


*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*
* Correctly estimated AME by troop size 
*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*

	
*model 2a: autocracy
poisson milcoupsum robust_autocracy6##c.troops population2000 gdpcap durable conflict milexp exsol, cluster(ccode)
margins, dydx(robust_autocracy6) at (troops = (0(0.05)9.8)) post
marginsplot, ///
	recast(line) plot1opts(lcolor(gs8)) ciopt(color(black%20)) recastci(rarea) ///
	ytitle("AME", size(medsmall)) ///
	xtitle("Troops", size(medsmall))  ///
	title("Autocracy", size(medium)) yline(0, lcolor(red)) ///
	addplot(hist troops, /// adding histogram marginsplot graph
blcolor(black%30) fcolor(white%10) /// bar line and fill colors
percent /// histogram bins in "percent" rather than "discrete" (actual)
yaxis(2) /// calls for 2nd y-axis
yscale(alt lcolor() axis(2)) /// scaling on 2nd y-axis
ylabel(0 "0%" 20 "20%" 40 "40%" 60 "60%" 80 "80%" 100 "100%", /// labels on 2nd y-axis
labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small)) /// label options on 2nd y-axis
ytitle(" ", axis(2)))  legend(off)
graph save "figures\Study_2\B2.2.gph", replace


*model 4a: anocracy
poisson milcoupsum robust_anocracy5##c.troops population2000 gdpcap durable conflict milexp exsol, cluster(ccode)
margins, dydx(robust_anocracy5) at (troops =  (0(0.05)9.8)) post
marginsplot, ///
	recast(line) plot1opts(lcolor(gs8)) ciopt(color(black%20)) recastci(rarea) ///
	ytitle("AME", size(medsmall)) ///
	xtitle("Troops", size(medsmall))  ///
	title("Anocracy", size(medium)) yline(0, lcolor(red)) ///
	addplot(hist troops, /// adding histogram marginsplot graph
blcolor(black%30) fcolor(white%10) /// bar line and fill colors
percent /// histogram bins in "percent" rather than "discrete" (actual)
yaxis(2) /// calls for 2nd y-axis
yscale(alt lcolor() axis(2)) /// scaling on 2nd y-axis
ylabel(0 "0%" 20 "20%" 40 "40%" 60 "60%" 80 "80%" 100 "100%", /// labels on 2nd y-axis
labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small)) /// label options on 2nd y-axis
ytitle(" ", axis(2)))  legend(off)
graph save "figures\Study_2\B2.4.gph", replace


*model 8a: New democracy
eststo: poisson milcoupsum new_robust_democracy6##c.troops population2000 gdpcap durable conflict milexp exsol, cluster(ccode)
margins, dydx(new_robust_democracy6) at (troops =  (0(0.05)9.8)) post
marginsplot, ///
	recast(line) plot1opts(lcolor(gs8)) ciopt(color(black%20)) recastci(rarea) ///
	ytitle("AME", size(medsmall)) ///
	xtitle("Troops", size(medsmall))  ///
	title("New democracy", size(medium)) yline(0, lcolor(red)) ///
	addplot(hist troops, /// adding histogram marginsplot graph
blcolor(black%30) fcolor(white%10) /// bar line and fill colors
percent /// histogram bins in "percent" rather than "discrete" (actual)
yaxis(2) /// calls for 2nd y-axis
yscale(alt lcolor() axis(2)) /// scaling on 2nd y-axis
ylabel(0 "0%" 20 "20%" 40 "40%" 60 "60%" 80 "80%" 100 "100%", /// labels on 2nd y-axis
labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small)) /// label options on 2nd y-axis
ytitle(" ", axis(2)))  legend(off) 
graph save "figures\Study_2\B2.8.gph", replace


graph combine "figures\Study_2\B2.2.gph" "figures\Study_2\B2.4.gph" "figures\Study_2\B2.8.gph", col(3) title("C", position(11))
graph save "figures\Study_2\Figure_B3_AME_by_troop_rounded.gph", replace


*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*
* Figure 2 Main Paper
*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*


graph combine "figures\Study_2\Figure_2_original.gph" "figures\Study_2\Figure_2_AME_Regime_Type.gph", col(2)
graph save "figures\Study_2\Figure_work_around.gph", replace


graph combine "figures\Study_2\Figure_work_around.gph" "figures\Study_2\Figure_B3_AME_by_troop_rounded.gph", col(1)
graph export "figures\Study_2\Figure_2_Main_Paper.png", as(png) name("Graph") replace



